package com.supply.dl.utils;

import android.content.Context;

import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.OperateFlagEnum;
import com.supply.dl.bean.request.SaveJobInfoDetailRequest;
import com.supply.dl.bean.request.ConfirmLocationNoticeDetailRequest;
import com.supply.dl.bean.request.SaveTransferGoodsRequest;
import com.supply.dl.bean.request.SaveWorkNoticeBoundDetailRequest;
import com.supply.dl.http.HttpManger;

/**
 * Created by ${qianzhihe} on 2018/7/4.
 * 公用接口工具类
 */

public class InterfaceUtil {

    private static InterfaceUtil intance;

    InterfacesCallBackListener mInterfacesCallBackListener;

    public HttpManger httpManger;

    public InterfaceUtil(){
        if (httpManger!=null){
            httpManger=null;
        }
        httpManger=HttpManger.getInstance();
    }

    public static InterfaceUtil getInstance(){
        if (null==intance){
            synchronized (InterfaceUtil.class){
                if (null==intance){
                    intance=new InterfaceUtil();
                }
            }
        }
        return intance;
    }


    /**
     * 通知单定位详情：
     * 确认定位按钮点击事件
     * 确认定位(通知单未定位)
     * 确认定位(通知单已定位)
     * @param noticeDetailRequest
     * @param flag 事件标识（确认定位、删除定位等）
     * @param context 上下文对象
     * @param isInBound 是否是入库
     */
    public void confirmLocationByNoticeLocationDetail(ConfirmLocationNoticeDetailRequest noticeDetailRequest, final OperateFlagEnum flag, Context context, boolean isInBound){
        httpManger.setContext(context.getApplicationContext());
        if (isInBound){
            httpManger.confirmLocationByNoticeLocationDetailInBound(noticeDetailRequest);
        }else {
            httpManger.confirmLocationByNoticeLocationDetailOutBound(noticeDetailRequest);
        }
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 通知单出入库详情：
     * 作业列表下item的组件点击事件
     * 确认作业按钮点击事件
     * @param boundDetailRequest
     * @param flag
     * @param context
     * @param isInbound
     */
    public void saveWorkNoticeBoundDetail(SaveWorkNoticeBoundDetailRequest boundDetailRequest, final OperateFlagEnum flag, Context context, boolean isInbound){
        httpManger.setContext(context.getApplicationContext());
        if (isInbound){
            httpManger.saveWorkNoticeBoundDetailInBound(boundDetailRequest);
        }else {
            httpManger.saveWorkNoticeBoundDetailOutBound(boundDetailRequest);
        }
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 通知单定位详情：
     * 删除按钮点击事件
     * 删除定位(通知单未定位)
     * 删除定位(通知单已定位)
     * @param ids 定位ID：多个ID已逗号分隔
     * @param flag 事件标识（确认定位、删除定位等）
     * @param context 上下文对象
     */
    public void deleteLocationByNoticeLocationDetail(String ids, final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.deleteLocationList(ids);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 出库定位--定位详情--删除定位
     * 3.7接口：删除定位
     * @param ids
     * @param flag
     * @param context
     */
    public void deleteLocationByNoticeLocationDetailOutBound(String ids, final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.deleteLocationByNoticeLocationDetailOutBound(ids);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     *  订单批量定位UI界面：确认定位(订单批量定位)
     *  2.16接口：wms/app/new/asn/position/confirm
     * @param noticeDetailRequest 请求参数对象
     * @param context
     */
    public void batchLocationUIConfirmLocationByOrderId(ConfirmLocationNoticeDetailRequest noticeDetailRequest, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.batchLocationUIConfirmLocationByOrderId(noticeDetailRequest);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message);
                }
            }
        });
    }

    /**
     *  通知单批量定位UI界面：确认定位(通知单批量定位)
     *  2.9接口：wms/app/new/asn/position/batch/confirm
     * @param noticeDetailRequest
     * @param context
     */
    public void batchLocationUIConfirmLocationByNoticeOrderId(ConfirmLocationNoticeDetailRequest noticeDetailRequest, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.batchLocationUIByConfirmLocation(noticeDetailRequest);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message);
                }
            }
        });
    }

    /**
     * 通知单批量定位
     * @param noticeOrderId 通知单ID
     * @param context
     */
    public void batchLocationNoticeUIByNoticeOrderId(String noticeOrderId,final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.batchLocationNoticeUIByNoticeOrderId(noticeOrderId);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo,flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 通知单批量定位
     * @param orderId 通知单ID为空时，传orderId
     * @param context
     */
    public void batchLocationNoticeUIByOrderId(String orderId,final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.batchLocationNoticeUIByOrderId(orderId);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo,flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 通知单批量作业
     * @param noticeOrderIdOrOrderId 通知单ID或者订单ID
     * @param context
     * @param isShowNoticeUI 是否显示通知单UI界面
     */
    public void batchWorkByNoticeUI(String noticeOrderIdOrOrderId, final OperateFlagEnum flag, Context context, boolean isShowNoticeUI){
        httpManger.setContext(context.getApplicationContext());
        httpManger.batchWorkByNoticeUIOrOrderUI(noticeOrderIdOrOrderId, isShowNoticeUI);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }


    /**
     * 移库发货详情：
     * 作业列表下item的组件点击事件
     * 确认作业按钮点击事件
     * @param boundDetailRequest
     * @param flag
     * @param context
     * @param isWorkDone
     */
    public void saveJobNoticeBoundDetail(SaveJobInfoDetailRequest boundDetailRequest, final OperateFlagEnum flag, Context context, boolean isWorkDone){
        httpManger.setContext(context.getApplicationContext());
        if (isWorkDone){
            httpManger.saveHasJobInfo(boundDetailRequest);
        }else {
            httpManger.saveNotJobInfo(boundDetailRequest);
        }
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }



    /**
     * 移库收货详情：
     * 作业列表下item的组件点击事件
     * 确认作业按钮点击事件
     * @param boundDetailRequest
     * @param flag
     * @param context
     * @param isWorkDone
     */
    public void saveTransferGoodsDetail(SaveTransferGoodsRequest boundDetailRequest, final OperateFlagEnum flag, Context context, boolean isWorkDone){
        httpManger.setContext(context.getApplicationContext());
        if (isWorkDone){
            httpManger.saveTransferGoodsHasJobInfos(boundDetailRequest);
        }else {
            httpManger.saveTransferGoodsNotJobInfos(boundDetailRequest);
        }
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }



    /**
     * 移库发货详情：
     * 删除按钮点击事件
     * 删除作业
     * @param ids 定位ID：多个ID已逗号分隔
     * @param flag 事件标识（删除未作业、已作业等）
     * @param context 上下文对象
     */
    public void deleteMoveHouseGoodsWork(String ids, final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.deleteWorks(ids);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }

    /**
     * 移库收货详情：
     * 删除按钮点击事件
     * 删除作业
     * @param ids 定位ID：多个ID已逗号分隔
     * @param flag 事件标识（删除未作业、已作业等）
     * @param context 上下文对象
     */
    public void deleteTransferGoodsWork(String ids, final OperateFlagEnum flag, Context context){
        httpManger.setContext(context.getApplicationContext());
        httpManger.deleteTansferGoodsWorks(ids);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onSuccess(baseInfo, flag);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mInterfacesCallBackListener!=null){
                    mInterfacesCallBackListener.onFailure(message, flag);
                }
            }
        });
    }



    public interface InterfacesCallBackListener{
        void onSuccess(BaseInfo<?> baseInfo);

        void onFailure(String message);

        void onSuccess(BaseInfo<?> baseInfo, Enum opertateFlagEnum);

        void onFailure(String message, Enum opertateFlagEnum);
    }

    public void setInterfaceCallBackListener(InterfacesCallBackListener callBackListener){
        this.mInterfacesCallBackListener=callBackListener;
    }

    private HttpManger.HttpResponseCallBackListener mHttpResponseCallBackListener=new HttpManger.HttpResponseCallBackListener() {
        @Override
        public void onSuccess(BaseInfo<?> baseInfo) {
            if (mInterfacesCallBackListener!=null){
                mInterfacesCallBackListener.onSuccess(baseInfo);
            }
        }

        @Override
        public void onFailure(String message) {
            if (mInterfacesCallBackListener!=null){
                mInterfacesCallBackListener.onFailure(message);
            }
        }
    };

}
